import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  pathname: '',
  device: 'desktop',
  permission: false,
  tempVisible: false,
  websoketIsOpen: false,
  is_smk_btn_disable: false
}

const mutations = {
  IS_SMK_BTN_DISABLE: (state, value) => {
    state.is_smk_btn_disable = value
  },
  WEBSOKETISOPEN: (state, value) => {
    state.websoketIsOpen = value
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  OPEN_SIDEBAR: (state) => {
    Cookies.set('sidebarStatus', 1)
    state.sidebar.opened = true
    state.sidebar.withoutAnimation = false
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_PATHNAME: (state, pathname) => {
    state.pathname = pathname
  },
  SET_PERMISSION: (state, permission) => {
    state.permission = permission
  },
  SET_tempVisible: (state, value) => {
    state.tempVisible = value
  }
}

const actions = {
  ISSMKBTNDISABLE({ commit }, value) {
    commit('IS_SMK_BTN_DISABLE', value)
  },
  setWebsoketisOpen({ commit }, value) {
    commit('WEBSOKETISOPEN', value)
  },
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  openSideBar({ commit }) {
    commit('OPEN_SIDEBAR')
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setPathname({ commit }, pathname) {
    commit('SET_PATHNAME', pathname)
  },
  setPmt({ commit }, permission) {
    commit('SET_PERMISSION', permission)
  },
  toggleTempVisible({ commit }, value) {
    commit('SET_tempVisible', value)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
